
package com.hd.sup.modules.alarm.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.hd.common.constant.BizConstant;
import com.hd.common.page.BaseController;
import com.hd.common.reqres.request.RequestData;
import com.hd.common.reqres.response.Result;
import com.hd.common.utils.UserUtils;
import com.hd.sup.modules.alarm.model.AlarmDailyStatis;
import com.hd.sup.modules.alarm.service.IAlarmDailyStatisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title AlarmController.java
 * @Description 报警控制器
 * @Package com.hd.sup.modules.alarm.controller
 * @date 2020/12/28 2:32 下午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@RestController
@RequestMapping("/alarm")
public class AlarmController extends BaseController {

    @Resource
    private UserUtils userUtils;

    @Resource
    private IAlarmDailyStatisService alarmDailyStatisService;

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 统计报警类型（当天：关联 create_time）
     * @date 2020/12/28 11:23 上午
     * @params [world]
     */
    @PostMapping("/alarmTypeStatis")
    public Result<?> alarmTypeStatis(@RequestBody RequestData requestData) {
        String currentDate = requestData.getString("currentDate");
        if (StringUtils.isBlank(currentDate)) return Result.error("参数 currentDate 为空");

        Map<String, Object> resultMap = alarmDailyStatisService.getDailyAlarmTypeSum(currentDate, userUtils.getDeptIdList());

        return Result.success(resultMap);
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 告警趋势统计
     * @date 2020/12/28 6:34 下午
     * @params []
     */
    @PostMapping("/alarmTrend")
    public Result<?> alarmTrend(@RequestBody RequestData requestData) {

        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String regionCode = requestData.getString("regionCode");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");

        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return Result.error("参数范围时间和起始时间不允许同时选择");
        AlarmDailyStatis param = new AlarmDailyStatis(enterpriseNo, enterpriseName, regionCode, startDateStr, endDateStr, dateRange);

        Map<String, Object> resultMap = Maps.newHashMap();

        //填充报警趋势数据
        for (BizConstant.AlarmType one : BizConstant.AlarmType.values()) {
            param.setAlarmType(one.key());
            List<Map<String, Object>> data = alarmDailyStatisService.getTrendData(param);
            resultMap.put(one.property(), data);
        }

        return Result.success(resultMap);
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 报警企业处置率top5
     * @date 2020/12/29 4:04 下午
     * @params []
     */
    @GetMapping("/alarmHandleRateTop5")
    public Result<?> alarmHandleRateTop5() {
        List<String> deptIds = userUtils.getDeptIdList();
        List<Map<String, Object>> result = alarmDailyStatisService.getHandleRateTop5(deptIds);
        return Result.success(result);
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 报警企业表格
     * @date 2020/12/30 11:56 上午
     * @params []
     */
    @PostMapping("/alarmEnterprise")
    public Result<?> alarmEnterprise(@RequestBody RequestData requestData) {

        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String regionCode = requestData.getString("regionCode");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");

        String sort = requestData.getString("sort");
        String order = requestData.getString("order");
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return Result.error("参数范围时间和起始时间不允许同时选择");
        if (current == null || size == null) return Result.error("参数 current 或 size 不允许为空");

        AlarmDailyStatis param = new AlarmDailyStatis(enterpriseNo, enterpriseName, regionCode, startDateStr, endDateStr, dateRange, sort, order);

        Page<Map<String, Object>> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        List<Map<String, Object>> result = alarmDailyStatisService.getAlarmEnterprisePage(page, param);
        page.setRecords(result);

        return Result.success(page);
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 统计区域报警信息
     * @date 2020/12/31 9:13 上午
     * @params [requestData]
     */
    @PostMapping("/regionAlarm")
    public Result<?> regionAlarm(@RequestBody RequestData requestData) {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String regionCode = requestData.getString("regionCode");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");

        String sort = requestData.getString("sort");
        String order = requestData.getString("order");
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return Result.error("参数范围时间和起始时间不允许同时选择");
        if (current == null || size == null) return Result.error("参数 current 或 size 不允许为空");

        AlarmDailyStatis param = new AlarmDailyStatis(enterpriseNo, enterpriseName, regionCode, startDateStr, endDateStr, dateRange, sort, order);

        Page<Map<String, Object>> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        List<Map<String, Object>> result = alarmDailyStatisService.getAlarmRegionPage(page, param);
        page.setRecords(result);


        return Result.success(page);
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 区域报警处置率表格分页信息
     * @date 2020/12/31 11:05 上午
     * @params [requestData]
     */
    @PostMapping("/regionHandleRateAlarm")
    public Result<?> regionHandleRateAlarm(@RequestBody RequestData requestData) {

        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String regionCode = requestData.getString("regionCode");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return Result.error("参数范围时间和起始时间不允许同时选择");
        if (current == null || size == null) return Result.error("参数 current 或 size 不允许为空");

        AlarmDailyStatis param = new AlarmDailyStatis(enterpriseNo, enterpriseName, regionCode, startDateStr, endDateStr, dateRange);

        Page<Map<String, Object>> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        List<Map<String, Object>> result = alarmDailyStatisService.getRegionAlarmHanderRatePage(page, param);
        page.setRecords(result);

        return Result.success(page);
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 查询企业报警处置率分页信息
     * @date 2020/12/31 11:04 上午
     * @params [requestData]
     */
    @PostMapping("/enterpriseHandleRateAlarm")
    public Result<?> enterpriseHandleRateAlarm(@RequestBody RequestData requestData) {

        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String regionCode = requestData.getString("regionCode");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return Result.error("参数范围时间和起始时间不允许同时选择");
        if (current == null || size == null) return Result.error("参数 current 或 size 不允许为空");

        AlarmDailyStatis param = new AlarmDailyStatis(enterpriseNo, enterpriseName, regionCode, startDateStr, endDateStr, dateRange);

        Page<Map<String, Object>> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        List<Map<String, Object>> result = alarmDailyStatisService.getEnterpriseAlarmHanderRatePage(page, param);
        page.setRecords(result);

        return Result.success(page);
    }

}
